<?php

/**
 * @Description    系统参数设置
 * @Author         zhaojiping
 * @Copyright      Copyright (c) 山东艾思软件科技有限公司(https://www.aisisofit.cn)
 * @Date           2021/1/20
 * @CreateBy       VS Code
 */

namespace app\core\controller;

use app\core\model\CoreConfig;
use app\common\controller\CommonController;

class ConfigController extends CommonController {

    protected $model = null;
    protected $validate = null;

    /**
     * 初始化
     */
    public function initialize() {
        parent::initialize();
        $this->model = new CoreConfig();
        $this->validate = new \app\core\validate\CoreConfig();
    }

    /**
     * 获取数据配置列表
     */
    public function lists() {
        $keyword = input('keyword', '');

        $where = [];
        if ($keyword != '') $where[] = ['name|title|value|describe', 'like', "%$keyword%"];

        $list = $this->model->getPageList($where);

        json_return(1, '获取成功', $list);
    }

    /**
     * 系统设置
     */
    public function setting() {
        $config_group_list = $this->model->where(['name' => 'config_group_list'])->value('value');
        $config_group_list = parse_config_attr($config_group_list);
        $list = [];
        $field = ['id', 'title', 'name', 'value', 'type', 'describe', 'extral'];
        foreach ($config_group_list as $k => $v) {
            $list[$k]['id'] = $k;
            $list[$k]['title'] = $v;
            $item = $this->model->where(['group' => $k])->order('sort, id')->field($field)->select();
            $list[$k]['item'] = $item;
            foreach ($item as $ke => $val) {
                if ($item[$ke]['extral'] != '') {
                    $extral = parse_config_attr($val['extral']);
                    $array = [];
                    foreach ($extral as $key => $value) {
                        $array[$key]['id'] = $key;
                        $array[$key]['name'] = $value;
                    }
                    $list[$k]['item'][$ke]['extral'] = $array;
                }
            }
        }

        json_return(1, '系统设置', $list);
    }

    /**
     * 配置系统管理
     */
    public function editSetting() {
        $param = input();
        unset($param['token']);

        if (empty($param)) json_return(0, '信息没有获取到');

        $list = array();
        foreach ($param as $key => $value) {
            if (!empty($param[$key]['item'])) {
                foreach ($param[$key]['item'] as $k => $v) {
                    array_push($list, $param[$key]['item'][$k]);
                }
            }
        }

        foreach ($list as $key => $value) {
            $data['value'] = $list[$key]['value'];
            $where['id'] = $list[$key]['id'];
            $this->model->where($where)->save($data);
            // $arr[$key] = $this->model->getLastSql();
        }

        cache('config', null);
        cache('init_config', null);

        json_return(1, '保存成功');
    }

    /**
     * 获取类型配置
     */
    public function getConfigTypeList() {
        $config_type_list = $this->model->where(array('name' => 'config_type_list'))->value('value');
        $config_type_list = parse_config_attr($config_type_list);
        $list = [];
        foreach ($config_type_list as $key => $value) {
            $list[$key]['id'] = $key;
            $list[$key]['name'] = $value;
        }
        return json_return(1, '参数类型', $list);
    }

    /**
     * 获取分组配置
     */
    public function getConfigGroupList() {
        $config_group_list = $this->model->where(array('name' => 'config_group_list'))->value('value');
        $config_group_list = parse_config_attr($config_group_list);
        $list = [];
        foreach ($config_group_list as $key => $value) {
            $list[$key]['id'] = $key;
            $list[$key]['name'] = $value;
        }
        return json_return(1, '参数分组', $list);
    }
}
